$(function () {
    $("#jqGrid").Grid({
        url: '../devplanrule/list',
        colModel: [
            {label: '日期顺序', name: 'dayNum', index: 'DAY_NUM', width: 80},
            {label: '月顺序', name: 'monNum', index: 'MON_NUM', width: 80},
            {label: '执行人', name: 'carryer', index: 'CARRYER', width: 80},
            {label: '规程类型保留字段', name: 'ruleType', index: 'RULE_TYPE', width: 80},
            {label: '所属车间', name: 'dept', index: 'DEPT', width: 80},
            {label: '执行人接受时间', name: 'carryNameDate', index: 'CARRY_NAME_DATE', width: 80},
            {label: '完成时间', name: 'completeDate', index: 'COMPLETE_DATE', width: 80},
            {label: '执行时间', name: 'carryDate', index: 'CARRY_DATE', width: 80},
            {label: '是否删除 0已删除 1未删除', name: 'delFlag', index: 'DEL_FLAG', width: 80},
            {label: '规程ID', name: 'ruleId', index: 'RULE_ID', width: 80},
            {label: '维护计划ID', name: 'planId', index: 'PLAN_ID', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        devPlanRule: {
            dayNum: '',
            monNum: '',
            carryer: '',
            ruleType: '',
            dept: '',
            carryNameDate: '',
            completeDate: '',
            carryDate: '',
            delFlag: '',
            ruleId: '',
            planId: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.devPlanRule = {
                dayNum: '',
                monNum: '',
                carryer: '',
                ruleType: '',
                dept: '',
                carryNameDate: '',
                completeDate: '',
                carryDate: '',
                delFlag: '',
                ruleId: '',
                planId: '',
                id: '',
            };
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            let url = vm.devPlanRule.id == (null || '') ? "../devplanrule/save" : "../devplanrule/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.devPlanRule),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../devplanrule/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../devplanrule/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.devPlanRule = r.devPlanRule;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});